package org.jeecg.modules.gqHealth.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.gqHealth.entity.GqCardManager;
import org.jeecg.modules.gqHealth.entity.GqStatisticsConfig;
import org.jeecg.modules.gqHealth.service.IGqCardManagerService;
import org.jeecg.modules.gqHealth.service.IGqRealnameInfoService;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 体检卡表管理
 * @Author: jeecg-boot
 * @Date:   2024-05-12
 * @Version: V1.0
 */
@Api(tags="体检卡表管理")
@RestController
@RequestMapping("/gqHealth/gqCardManager")
@Slf4j
public class GqCardManagerController extends JeecgController<GqCardManager, IGqCardManagerService> {
	@Autowired
	private IGqCardManagerService gqCardManagerService;


	
	/**
	 * 分页列表查询
	 *
	 * @param gqCardManager
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "体检卡表管理-分页列表查询")
	@ApiOperation(value="体检卡表管理-分页列表查询", notes="体检卡表管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<GqCardManager>> queryPageList(GqCardManager gqCardManager,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<GqCardManager> queryWrapper = QueryGenerator.initQueryWrapper(gqCardManager, req.getParameterMap());
		Page<GqCardManager> page = new Page<GqCardManager>(pageNo, pageSize);
		IPage<GqCardManager> pageList = gqCardManagerService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param gqCardManager
	 * @return
	 */
	@AutoLog(value = "体检卡表管理-添加")
	@ApiOperation(value="体检卡表管理-添加", notes="体检卡表管理-添加")
	//@RequiresPermissions("org.jeecg.modules:gq_card_manager:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody GqCardManager gqCardManager) {
		gqCardManagerService.save(gqCardManager);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param gqCardManager
	 * @return
	 */
	@AutoLog(value = "体检卡表管理-编辑")
	@ApiOperation(value="体检卡表管理-编辑", notes="体检卡表管理-编辑")
	//@RequiresPermissions("org.jeecg.modules:gq_card_manager:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody GqCardManager gqCardManager) {
		gqCardManagerService.updateById(gqCardManager);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "体检卡表管理-通过id删除")
	@ApiOperation(value="体检卡表管理-通过id删除", notes="体检卡表管理-通过id删除")
	//@RequiresPermissions("org.jeecg.modules:gq_card_manager:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		gqCardManagerService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "体检卡表管理-批量删除")
	@ApiOperation(value="体检卡表管理-批量删除", notes="体检卡表管理-批量删除")
	//@RequiresPermissions("org.jeecg.modules:gq_card_manager:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.gqCardManagerService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "体检卡表管理-通过id查询")
	@ApiOperation(value="体检卡表管理-通过id查询", notes="体检卡表管理-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<GqCardManager> queryById(@RequestParam(name="id",required=true) String id) {
		GqCardManager gqCardManager = gqCardManagerService.getById(id);
		if(gqCardManager==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(gqCardManager);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param gqCardManager
    */
    //@RequiresPermissions("org.jeecg.modules:gq_card_manager:exportXls")
	@ApiOperation(value="体检卡表管理-导出", notes="体检卡表管理-导出")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, GqCardManager gqCardManager) {
        return exportXls(request, gqCardManager, GqCardManager.class, "体检卡表管理");
    }

	/**
	 * 导出excel
	 *
	 * @param request
	 */
	protected ModelAndView exportXls(HttpServletRequest request, GqCardManager object, Class<GqCardManager> clazz, String title) {
		// Step.1 组装查询条件
		QueryWrapper<GqCardManager> queryWrapper = QueryGenerator.initQueryWrapper(object, request.getParameterMap());
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		queryWrapper.isNull("active_time");
		// 过滤选中数据
		String selections = request.getParameter("selections");
		if (oConvertUtils.isNotEmpty(selections)) {
			List<String> selectionList = Arrays.asList(selections.split(","));
			queryWrapper.in("batch",selectionList);
		}
		// Step.2 获取导出数据
		List<GqCardManager> exportList = service.list(queryWrapper);

		// Step.3 AutoPoi 导出Excel
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		//此处设置的filename无效 ,前端会重更新设置一下
		mv.addObject(NormalExcelConstants.FILE_NAME, title);
		mv.addObject(NormalExcelConstants.CLASS, clazz);
		//update-begin--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置--------------------
		ExportParams exportParams=new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title);
//		exportParams.setImageBasePath(upLoadPath);
		//update-end--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置----------------------
		mv.addObject(NormalExcelConstants.PARAMS,exportParams);
		mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
		return mv;
	}

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("gq_card_manager:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, GqCardManager.class);
    }

	 /**
	  *
	  * @param batch 批次号
	  * @param num 数量
	  * @return
	  */
	 @ApiOperation(value="体检卡表管理-生成卡号", notes="体检卡表管理-生成卡号")
	 @PostMapping("/generateCard")
	public Result<?> generateCard(@RequestParam("batch") String batch ,@RequestParam("num") Integer num)
	 {
		 Integer integer = gqCardManagerService.generateCard(batch, num);
		 if (integer == -1){
			 return Result.error("没有找到批次号");
		 }
		 if (integer == -2 ||integer == 0){
			 return Result.error("卡号生成失败，重试");
		 }
		 if (integer > 0){
			 return Result.OK("生成"+integer+"张卡");
		 }
		 return Result.error("卡号生成失败，重试");
	 }


	 /**
	  *
	  * @param no 卡号
	  * @param validate 校验码
	  * @return
	  */
	 @ApiOperation(value="体检卡表管理-用户绑卡", notes="体检卡表管理-用户绑卡")
	 @PostMapping("/bindUserCard")
	 public Result<?> bindUserCard(@RequestParam("no") String no , String validate)
	 {
		 Integer integer = gqCardManagerService.bindUserCard(no, validate);
		 if (integer == -1){
			 return Result.error("卡不存在或者被激活");
		 }

		 return Result.OK("绑定成功");
	 }

	 /**
	  *
	  * @return
	  */
	 @ApiOperation(value="体检卡表管理-查询用户绑卡", notes="体检卡表管理-查询用户绑卡")
	 @GetMapping("/selectUserCard")
	 public Result<?> selectUserCard()
	 {
		 List<GqCardManager> gqCardManagers = gqCardManagerService.selectUserCard();
		 return Result.OK(gqCardManagers);
	 }


	/**
	 *
	 * @return
	 */
	@ApiOperation(value="体检卡表管理-查询PC首页统计数据", notes="体检卡表管理-查询PC首页统计数据")
	@GetMapping("/statisticsData")
	public Result<?> statisticsData()
	{

		GqStatisticsConfig config = gqCardManagerService.selectTotal();
		return Result.OK(config);
	}


 }
